<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 18/12/16
 * Time: 下午4:30
 */

namespace app\index\controller;


use think\Controller;
use think\Db;
use think\Request;

class Tempindex extends Controller
{
    public function add()
    {
        return view();
    }

    //获取数据库中数据渲染到视图中
    public function tempindex(){
        $this->data();
        return view();
    }


    //插入纸种信息
    public function jiancha()
    {
        $request = $this->request;
        $paper = $request->post('paper');
        $weight = $request->post('weight');
        $pid = $request->post('pid');

//        if (!preg_match('/^[\u4e00-\u9fa5]{0,}$/',$paper)){
//            echo '不是汉字';
//            exit;
//        };

//        print_r($paper);
        if ($request->isPost()){
           $a = Db::connect('db_config1')
               ->table('paper')
               ->where('paperName',$paper)
               ->select();
           $newdata[] = '';
           foreach ($a as $v){
               $newdata[] = $v['paperName'];
           }

           if(!in_array($paper,$newdata)){
             $res = Db::connect('db_config1')->table('paper')->insert(['paperName'=> $paper,'pid' => $pid]);
             print_r($res);
             if ($res){
                 $this->success('插入成功');
             }
            }else{
               $this->error('该纸种已存在,请重新输入');

           }
        }

    }

    //插入温度信息
    public function insert(){
        //获取数据库纸种信息
        $this->data();


        return view();
    }

    //
    public function into()
    {
        $data = $this->request->param();
        print_r($data);

        $res = Db::connect('db_config1')->table('temp')->insert($data);
        if ($res){
            $this->success('插入成功');
        }else{
            $this->error('插入失败');
        }

    }

    //ajax查询数据
    public function check()
    {
        $request = $this->request;
        $data2 = $request->param();

//        print_r($data2);

        $all = Db::connect('db_config1')
            ->table('temp')
            ->where($data2)
            ->find();
        $aa = json_encode($all);
         return $aa;

    }





    //获取数据库纸种信息渲染到视图中
    protected function data(){
        $data = Db::connect('db_config1')
            ->table('paper')
            ->field('paperName,pid')
            ->select();
//        print_r($data);
        $this->assign('data',$data);
        $weight = Db::connect('db_config1')
            ->table('weight')
            ->field('weight')
            ->select();
        $this->assign('weight',$weight);
    }



}